package com.ncp.model.biz.bbs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.comment.Replybean;
public class ReplyProcess {
	
	private Connection conn=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	private int pageCount=0;
	private int rowCount=0;
	private int pageSize=10;
	
	
	/**
	 * 得到某个帖子的所有回复
	 * @param id
	 * @param nowPage当前页数
	 * @return
	 */
	public ArrayList<Replybean> getRepaly(int id,int nowPage){
		ArrayList<Replybean> list=new ArrayList<Replybean>();
		String sql="select * from reply where rootid="+id+" limit "+pageSize*(nowPage-1)+","+pageSize+"";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				Replybean rb=new Replybean();
				rb.setId(rs.getInt("id"));
				rb.setPid(rs.getInt("pid"));
				rb.setRootid(rs.getInt("rootid"));
				rb.setUsername(rs.getString("username"));
				rb.setReplay(rs.getString("replay"));
				rb.setPdate(rs.getTimestamp("pdate"));
				list.add(rb);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
			new DB().closeSQL(conn, ps);
		}
		return list;
	}
	
	public Replybean replyDetail(int id){
		Replybean rb=new Replybean();
		String sql="select username,replay,rootid from reply where id="+id;
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				rb.setRootid(rs.getInt("rootid"));
				rb.setReplay(rs.getString("replay"));
				rb.setUsername(rs.getString("username"));
				
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
			new DB().closeSQL(conn, ps);
		}
		return rb;
	}
	
	/**
	 * 提交回复
	 * @param id
	 * @param rootid
	 * @param reply
	 * @param name 
	 * @return
	 */
	public boolean PostReply(int id,int rootid,String reply, String name){
		boolean b=false;
		String sql="insert into reply value(null,"+id+","+rootid+",'"+reply+"','"+name+"',now())";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			int n=ps.executeUpdate();
			if(n!=0){
				b=true;
			}	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
			new DB().closeSQL(conn, ps);
		}
		return b;
	}
	
	/**
	 * 提交主题
	 * @param id
	 * @param reply
	 * @return
	 */
	public boolean PostReply(int id,String reply,String name){
		boolean b=false;
		String sql="insert into reply value(null,0,"+id+",'"+reply+"','"+name+"',now())";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			int n=ps.executeUpdate();
			if(n!=0){
				b=true;
			}	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			new DB().closeSQL(conn, ps);
		}
		return b;
	}
	
	/**
	 * 删除回复
	 * @param id
	 * @return
	 */
	public boolean deleteReply(int id){
		boolean b=false;
		String sql="delete from reply where id="+id;
		String sql2="update reply set pid=0 where pid="+id;
		String sql3="select id from reply where pid="+id;
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			int n=ps.executeUpdate();
			if(n!=0){
				ps=conn.prepareStatement(sql3);
				rs=ps.executeQuery();
				if(rs.next()){
					ps=conn.prepareStatement(sql2);
					ps.executeUpdate();
				}
				b=true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
			new DB().closeSQL(conn, ps);
		}
		return b;
	}
	
	/**
	 * 得到某个帖子回复的总数量
	 * @param id
	 * @return
	 */
	public int getReplyNum(int id){
		int count=0;
		String sql="select count(*) from reply where rootid="+id;
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				count=rs.getInt(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
					rs=null;
				}
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
			new DB().closeSQL(conn, ps);
		}
		return count;
	}
	
	/**
	 * 得到评论的总页数
	 * @param ProductID
	 * @return
	 */
	public int getCount(int ProductID){
		rowCount=this.getReplyNum(ProductID);
		if(rowCount%pageSize==0){
			pageCount=rowCount/pageSize;
		}else if(rowCount%pageSize!=0){
			pageCount=rowCount/pageSize+1;
		}
		return pageCount;
	}
}
